/*
 * Copyright (C) 2008 OpenIntents.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tools.cleanmaster.utils;

import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import java.util.HashMap;
import java.util.Map;

public class MimeTypes {
    public static final String MIME_APK = "application/vnd.android.package-archive";
    // MS word
    public static final String MIME_MS_WORD = "application/msword";
    public static final String MIME_MS_WORD_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String MIME_MS_WORD_DOTX = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
    // MS execl
    public static final String MIME_MS_EXCEL = "application/vnd.ms-excel";
    public static final String MIME_MS_EXCEL_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String MIME_MS_EXCEL_XLTX = "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
    // MS Power point
    public static final String MIME_MS_POWERPOINT = "application/vnd.ms-powerpoint";
    public static final String MIME_MS_POWERPOINT_PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    public static final String MIME_MS_POWERPOINT_POTX = "application/vnd.openxmlformats-officedocument.presentationml.template";
    public static final String MIME_MS_POWERPOINT_PPSX = "application/vnd.openxmlformats-officedocument.presentationml.slideshow";

    // Flac
    public static final String MIME_FLAC = "application/x-flac";

    private static MimeTypes sInstance = new MimeTypes();

    public static synchronized MimeTypes getInstance() {
        if (sInstance == null) {
            sInstance = new MimeTypes();
        }

        return sInstance;
    }

    private Map<String, String> mMimeTypes;

    private MimeTypes() {
        initMimeTypesMap();
    }

    private void initMimeTypesMap() {
        mMimeTypes = new HashMap<String, String>();
        mMimeTypes.put(".opus", "audio");
        mMimeTypes.put(".ogg","audio");
        // 在此添加需要定制的MIME类型，示例：mMimeTypes.put(".apk", MIME_APK);
    }

    /**
     * @param filename
     * @return null will be returned if no MIME type found for this file.
     */
    public String getMimeType(String filename) {
        String extension = getExtension(filename);

        if (!TextUtils.isEmpty(extension)) {
            // Check ourself's map
            // Convert extensions to lower case letters for easier
            // comparison
            extension = extension.toLowerCase();
            if (mMimeTypes.containsKey(extension)) {
                return mMimeTypes.get(extension);
            }

            // Let's check the official map. Webkit has a nice extension-to-MIME
            // map.
            // Be sure to remove the first character from the extension, which
            // is
            // the "." character.
            String webkitMimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(extension.substring(1));

            if (webkitMimeType != null) {
                // Found one. Let's take it!
                return webkitMimeType;
            } else {
                return "";
            }
        }

        return "";
    }

    /**
     * Gets the extension of a file name, like ".png" or ".jpg".
     *
     * @param uri
     * @return Extension including the dot("."); "" if there is no extension;
     *         null if uri was null.
     */
    public static String getExtension(String uri) {
        if (uri == null) {
            return null;
        }

        int dot = uri.lastIndexOf(".");
        if (dot >= 0) {
            return uri.substring(dot);
        } else {
            // No extension.
            return "";
        }
    }
}
